<?php

/**
 *
 * SGN Bootstrap
 *
 *
 * @author deathemperor
 * @version $Id: Bootstrap.php 5187 2012-12-18 03:44:28Z locth $
 *
 *
 */
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{

    /**
     * Bootrap for guild_library
     */
    protected function _initLibraryAutoloader()
    {   
        $autoloader = new Zend_Loader_Autoloader_Resource(
        		array(
        				'basePath'      => APPLICATION_PATH,
        				'namespace'     => '',
        				'resourceTypes' => array (
        						'myrunmodel' => array (
        								'namespace' => 'Myrun_Model',
        								'path'      => 'modules/myrun/models',
        						),
        				)
        		)
        );
    }
    /**
     *
     * Initialize Environment
     *
     * production
     * staging
     * testing
     * development
     *
     * @return void
     *
     */
    protected function _initEnv()
    {
        switch (APPLICATION_ENV) {
            case 'development':
                @ini_set('display_errors', true);
                error_reporting(E_ALL & ~ E_NOTICE);
                break;
            case 'production':
                @ini_set('display_errors', false);
                error_reporting(E_ALL & ~ E_NOTICE);
                if (preg_match('/10\.199\.5\..*/', $_SERVER['REMOTE_ADDR'])) {
                    @ini_set('display_errors', true);
                    error_reporting(E_ALL & ~ E_NOTICE);
                }
                break;
        }

        if (function_exists('get_magic_quotes_runtime') && get_magic_quotes_runtime()) {
            @set_magic_quotes_runtime(false);
        }
        ignore_user_abort(true);
        @ini_set('output_buffering', false);
        while (@ob_end_clean());
    }

    /**
     *
     * Setup application variables
     *
     * @return void
     *
     */
    protected function _initSetup()
    {
        // add library/SGN into autoloader
        Zend_Controller_Action_HelperBroker::addPrefix('SGN_Helper');
        
        $startUpConfigKey = APPLICATION_ENV . '.BootstrapConfiguration';
        // raw APC cache configuration
        $configuration = apc_fetch($startUpConfigKey);
        if ($configuration === false || APPLICATION_ENV == 'development') {
            $configuration = new Zend_Config_Ini(APPLICATION_PATH . '/modules/default/configs/' . APPLICATION_ENV . '/' . APPLICATION_ENV . '.global.ini', MYRUN_ENV);
            apc_store($startUpConfigKey, $configuration);
            $configKeys = apc_fetch('configuration.keys');
            $configKeys[$startUpConfigKey] = $startUpConfigKey;
            apc_store('configuration.keys', $configKeys);
        }

//         var_dump($configuration);die();
        SGN_Application::set('configuration', $configuration);
        SGN_Application::set('googleSiteVerification', $configuration->get('googleSiteVerification'));
        require_once 'GlobalCache.php';
        require_once 'Globals.php';
        require_once 'ResultCache.php';
        if (function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc()) {
            SGN_Application::undoMagicQuotes($_GET);
            SGN_Application::undoMagicQuotes($_POST);
            SGN_Application::undoMagicQuotes($_COOKIE);
            SGN_Application::undoMagicQuotes($_REQUEST);
        }
    }
    
    protected function _initLogging()
    {
        // the disabling based on users & environment is done on ChromePhp.php
//         require_once ROOT_PATH . '/library/ChromePhp.php';
    }

    /**
     *
     * Init View Related info
     */
    protected function _initDoctype()
    {
        $this->bootstrap('view');
        $view = $this->getResource('view');
        $view->doctype('XHTML1_STRICT');
        //add ZendX
        $view->addHelperPath('ZendX/JQuery/View/Helper/', 'ZendX_JQuery_View_Helper');
        $view->addHelperPath(APPLICATION_PATH.'/modules/sgn/views/helpers/', 'SGN_View_Helper_');
    }
    
    /**
     * Init route
     * @return void
     */
    protected function _initRoutes()
    {
    	$this->bootstrap('FrontController');
    	$front = $this->getResource('FrontController');
    	$router = new Zend_Controller_Router_Rewrite();
    	$moduleNames = array('myrun');
    	foreach($moduleNames as $name){
    	    $config = SGN_Application::getConfig($name, 'routes');
    	    $router->addConfig($config, 'routes');
    	}
    	$front->setRouter($router);
    }
    /**
     * 
     * Init site modules 
     * 
     */
    protected function _initSiteModules()
    {
        //Don't forget to bootstrap the front controller as the resource may not been created yet...
        $this->getResource("frontController")->addModuleDirectory(APPLICATION_PATH . '/modules');
    }
    /**
     *
     * Setup controllers
     *
     * @return void
     */
    protected function _initControllers()
    {
        $front = Zend_Controller_Front::getInstance();
        $front->dispatch();
        unset($front);
    }
    
    
    /**
     * @return array
     */
    private function loadRouteConfigs($moduleName)
    {
    	$dir = APPLICATION_PATH . '/modules/' . $moduleName .'/configs' . '/routes';
    	if(!is_dir($dir)){
    		return array();
    	}
    	$configFile = array();
    	$dirIterator = new DirectoryIterator($dir);
    	foreach ($dirIterator as $file){
    		if($file->isDot() || $file->isDir()){
    			continue;
    		}
    		$name = $file->getFilename();
    		if (preg_match('/^[^a-z]/i', $name) || ('CVS' == $name)
    				|| ('.svn' == strtolower($name))) {
    			continue;
    		}
    		$configFiles[] = $dir . '/' . $name;
    	}
    	return $configFiles;
    }
}
